// Widgets/Protocol/ProtocolAmount/ProtocolAmount.js
import { queryAppInforMation } from '../../../api/brand';
import { formatTime } from '../../../utils/util';
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    lendReason: {
      type: String,
      value: '', // 借款事由
    },
    dateCombo: {
      type: String,
      value: '', // 日期组合
    }
  },
  options: {
    styleIsolation: 'shared',
  },
  lifetimes: {
    attached: function() {
      let _this = this;
      this._getLendReason();
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    lendReasonList: [],
    show: false,
    showDate: false,
    maxDate: (new Date().getTime() + (1090 * 24 * 3600 * 1000))
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event){
      // event.detail 为当前输入的值
      console.log(event.detail);
      const name = event.target.dataset.name||event.currentTarget.dataset.name;
      console.log(name,'name')
      this.triggerEvent('updateValue',{'data': event.detail,'name': name})
    },
    // 选择借款事由
    selLendReason(){
      this.setData({show: true})
    },
    // 获取借款事由数据
    _getLendReason(){
      let _this = this;
      queryAppInforMation({infoType: '9',page: 0,size: 99})
      .then(res => {
        if(res.success){
          var arr = [];
          for (let index = 0; index < res.data.content.length; index++) {
            const element = res.data.content[index];
            arr.push(element.content);
          }
          _this.setData({
            lendReasonList: arr,
          })
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none'
          })
        }
      })
    },
    onClose(){
      this.setData({show: false,showDate: false})
    },
    onConfirm(event){
      const { picker, value, index } = event.detail;
      this.triggerEvent('updateValue',{'data': value,'name': 'lendReason'})
      this.onClose();
    },
    // 选择借款日期
    selDateCombo(){
      this.setData({showDate: true})
    },
    // 确认选择日期
    onDateConfirm(e){
      const [start, end] = e.detail;
      var startDate = formatTime(start,'Y-M-D');
      var endDate = formatTime(end,'Y-M-D');
      var dateCombo = `${startDate} ~ ${endDate}`;
      this.triggerEvent('updateValue',{'data': startDate,'name': 'startDate'})
      this.triggerEvent('updateValue',{'data': endDate,'name': 'endDate'})
      this.triggerEvent('updateValue',{'data': dateCombo,'name': 'dateCombo'})
      this.onClose();
    }
  }
})